import fetch from '@/utils/fetch'

export function fetchAppList(){
  return fetch({
    url: '/apps',
    method: 'get',
  })
}

export function fetchAppInfo(id){
  return fetch({
    url: `/apps/${id}/info`,
    method: 'get'
  })
}

export function createNewApp(appInfo){
  return fetch({
    url: '/apps',
    method: 'post',
    data: {
      ...appInfo
    }
  })
}

export function uploadAppPack(id, formdata) {
  return fetch({
    url: `/apps/${id}/package`,
    method: 'post',
    data: formdata
  })
}

export function uploadIcon(options) {
  var formdata = new FormData()
  formdata.append('image', options.file)
  return fetch({
    url: `/apps/${options.data.id}/images/icons`,
    method: 'post',
    data: formdata
  })
}

export function uploadMiniIcon(options) {
  var formdata = new FormData()
  formdata.append('image', options.file)
  return fetch({
    url: `/apps/${options.data.id}/images/mini-icons`,
    method: 'post',
    data: formdata
  })
}

export function uploadScreenshot(options) {
  var formdata = new FormData()
  formdata.append('image', options.file)
  return fetch({
    url: `/apps/${options.data.id}/images/screenshots`,
    method: 'post',
    data: formdata
  })
}

export function submitAppInfo(id, form1, form2) {
  return fetch({
    url: `/apps/${id}/info`,
    method: 'put',
    data: {
      ...form1,
      ...form2
    }
  })
}
